package meetingcostclock;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Locale;
import java.util.Properties;

public class SettingsProvider {
	private static final String SETTINGS_FILE = "meetingcostclock.properties";
	
	private static final String KEY_NO_OF_PARTICIPANTS = "meetingcostclock.noOfParticipants";
	private static final String KEY_RATE = "meetingcostclock.rate";
	private static final String KEY_LANGUAGE = "meetingcostclock.language";
	private static final String KEY_COUNTRY = "meetingcostclock.country";
	private static final String KEY_ONTOP = "meetingcostclock.ontop";

	private Properties props;

	public SettingsProvider() {
		try {
			this.props = new Properties();
			File f = new File(SETTINGS_FILE);
			if (f.exists()) {
				props.load(new FileInputStream(SETTINGS_FILE));
			} else {
				setAlwaysOnTop(true);
				setLocale(Locale.getDefault());
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(-1);
		}
	}
	public void save() {
		try {
			props.store(new FileOutputStream(SETTINGS_FILE), "Settings of MeetingCostClock");
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(-1);
		}
	}

	
	public Integer getNoOfParticipants() {
		String str = props.getProperty(KEY_NO_OF_PARTICIPANTS);
		if (str != null) return Integer.parseInt(str);
		return null;
	}
	public void setNoOfParticipants(Integer no) {
		props.remove(KEY_NO_OF_PARTICIPANTS);
		if (no != null) props.setProperty(KEY_NO_OF_PARTICIPANTS, no.toString());
	}

	public Integer getRate() {
		String str = props.getProperty(KEY_RATE);
		if (str != null) return Integer.parseInt(str);
		return null;
	}
	public void setRate(Integer no) {
		props.remove(KEY_RATE);
		if (no != null) props.setProperty(KEY_RATE, no.toString());
	}

	public Locale getLocale() {
		String str1 = props.getProperty(KEY_LANGUAGE);
		String str2 = props.getProperty(KEY_COUNTRY);
		if (str1 != null) return new Locale(str1, str2);
		return null;
	}
	public void setLocale(Locale locale) {
		props.remove(KEY_LANGUAGE);
		props.remove(KEY_COUNTRY);
		if (locale != null) {
			props.setProperty(KEY_LANGUAGE, locale.getLanguage());
			props.setProperty(KEY_COUNTRY, locale.getCountry());
		}
	}

	public boolean getAlwaysOnTop() {
		return Boolean.parseBoolean(props.getProperty(KEY_ONTOP));
	}

	public void setAlwaysOnTop(boolean onTop) {
		props.setProperty(KEY_ONTOP, Boolean.valueOf(onTop).toString());
	}
}
